<?php
include './jwt.php';
use Auth\JwtToken;
include './loadEnv.php';
use Env\load;

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

header('Content-Type: application/json;charset=UTF-8');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");

function getAuthorizationHeader() {
    if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
        return str_replace('Bearer ', '', $_SERVER['HTTP_AUTHORIZATION']);
    } elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
        return str_replace('Bearer ', '', $_SERVER['REDIRECT_HTTP_AUTHORIZATION']);
    }
    return false;
}

$jwt = getAuthorizationHeader();

if ($jwt != false) {
    $jwtHandler = new JwtToken();
    $decoded = $jwtHandler->validateJwt($jwt);
    if ($decoded) {
        echo json_encode(['code' => 200, 'status' => 'success', 'message' => 'Authorization successful']);
    } else {
        echo json_encode(['code' => 401, 'status' => 'error', 'message' => 'Authorization failed']);
    }
} else {
    echo json_encode(['code' => 401, 'status' => 'error', 'message' => 'Authorization failed']);
}
?>